package cn.edu.nuc.onlinestore.network;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import cn.edu.nuc.onlinestore.model.Goods;

public class TCPClient {

	Socket client = null;
	OutputStream out = null;
	DataOutputStream dos = null;
	InputStream in = null;
	BufferedReader dis = null;
    ObjectInputStream ois=null;
    ObjectOutputStream oos=null;
	
    public TCPClient() {

		try {
			client = new Socket("127.0.0.1", 6666);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String send(String msg) {

		
		try {
			out = client.getOutputStream();
			dos = new DataOutputStream(out);

			in = client.getInputStream();
			dis = new BufferedReader(new InputStreamReader(in));

			dos.writeUTF(msg);
			String backmsg = dis.readLine();
			return backmsg;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {

				dis.close();
				in.close();

				dos.close();
				out.close();
				client.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "error";

	}


	
	public  List<Goods> allGoods() {
       
		String msg="allgoods###";
		List<Goods> allList=new ArrayList<Goods>();
		try {
			
			out = client.getOutputStream();
			dos = new DataOutputStream(out);

			in = client.getInputStream();
			//dis = new DataInputStream(in);
            
			dos.writeUTF(msg);
			
			ois =new ObjectInputStream(in);
			
			allList=(List<Goods>)ois.readObject();
			
			return allList;
		} catch (IOException | ClassNotFoundException  e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {

			
				in.close();
                ois.close();
				dos.close();
				out.close();
				client.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
        }
		return null;
		
		
	}
	
	
	public  HashSet<Goods> allCartGoods(String msg) {
	       
		//String msg="allCartgoods###";
		HashSet<Goods> allCartList=new HashSet<Goods>();
		try {
			
			out = client.getOutputStream();
			dos = new DataOutputStream(out);

			in = client.getInputStream();
			//dis = new DataInputStream(in);
            
			dos.writeUTF(msg);
			
			ois =new ObjectInputStream(in);
			
			allCartList=(HashSet<Goods>)ois.readObject();
			
			return allCartList;
		} catch (IOException | ClassNotFoundException  e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {

			
				in.close();
                ois.close();
				dos.close();
				out.close();
				client.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
        }
		return null;
		
		
	}
}